# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:ios)

before_all do
  # ensure_git_branch(branch: 'main')
  # ensure_git_status_clean
  # git_pull
end

react_native_path = File.join(__dir__, "..", "..", "node_modules", "react-native")

# package.json
package = JSON.parse(File.read(File.join(react_native_path, "package.json")))
version = package['version']

# .tar.gz文件路径
tarball_debug_path = "#{Dir.pwd}/react-native-artifacts-#{version}-hermes-ios-debug.tar.gz"
tarball_release_path = "#{Dir.pwd}/react-native-artifacts-#{version}-hermes-ios-release.tar.gz"

# 判断当前目录下是否存在.tar.gz文件
def tarball_exist?(path)
  File.exist?(path)
end


platform :ios do
  lane :poddebug do
    ENV['RCT_NEW_ARCH_ENABLED'] = "0"
    ENV['USE_HERMES'] = "1"
    ENV["NO_FLIPPER"] = "0"
    ENV['PRODUCTION'] == "0"
    # 删除 Podfile.lock 文件
    FileUtils.rm_f(File.join(__dir__, "..", "Podfile.lock"))

    # 如果这个文件存在，才执行下面的操作
    if tarball_exist?(tarball_debug_path)
      ENV["HERMES_ENGINE_TARBALL_PATH"] = tarball_debug_path
    end
    cocoapods
  end

  lane :podrelease do
    ENV['RCT_NEW_ARCH_ENABLED'] = "0"
    ENV['USE_HERMES'] = "1"
    ENV["NO_FLIPPER"] = "1"
    ENV['PRODUCTION'] == "1"
    ENV['USE_FRAMEWORKS'] = "static"

    # 删除 Podfile.lock 文件
    FileUtils.rm_f(File.join(__dir__, "..", "Podfile.lock"))

    # 如果这个文件存在，才执行下面的操作
    if tarball_exist?(tarball_release_path)
      ENV["HERMES_ENGINE_TARBALL_PATH"] = tarball_release_path
    end
    cocoapods
  end

  lane :gofir do
    gym(
      workspace: "SDHotelDataInsightApp.xcworkspace", 
      scheme: "SDHotelDataInsightApp(Production)",
      include_bitcode: false,
      include_symbols: false,
      silent: true,
      clean: true,
      export_method: "development",   # Available options: app-store ad-hoc package enterprise development
    )
    fir_cli(
      api_token: "ae1d65e6383eeb50b2723f85b5e30b2b",
      changelog: "release a new version",
      specify_file_path: lane_context[SharedValues::IPA_OUTPUT_PATH],
    )
  end

  lane :publish do
    podrelease
    gofir
  end
end
